<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/18
 * Time: 17:54
 */

namespace App\Game;

use App\Game\Traits\PbTrait;
use App\Models\Def\MainGamePlat;
use App\Models\QuotaHandicap;
use Carbon\Carbon;

class pb extends Game
{
    use PbTrait;
    private $platName = 'pb';
    public $timezone = 'GMT-4';

    const API_PLAYER_CREATE = '/player/create';
    const API_PLAYER_LOGIN = '/player/login';
    const API_PLAYER_BALANCE = '/player/info';
    const API_TRANSFER_IN = '/player/deposit';
    const API_TRANSFER_OUT = '/player/withdraw';
    const API_PLAYER_LOGOUT = '/player/logout';
    const API_BET_RECORD = '/report/all-wagers';


    public function __construct()
    {
        $this->adds = [
            'main_game_plat_code' => $this->platName,
            'plat_type' => 'sport',
        ];
    }

    /**
     * gameapi
     * 2018/9/11 10:13
     * Administrator
     * getLength
     *
     * @return mixed
     */
    public function getLength()
    {
        return $this->length;
    }

    /**
     * gameapi
     * 2018/9/11 0:08
     * Administrator
     * getPre
     * 获取前缀
     * @return mixed
     */
    public function getPre()
    {
        return $this->pre;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }


    /**
     * gameapi
     * 2019/1/16 17:31
     * Administrator
     * curlCreateMember
     * 创建账号
     * @param $accountUserName
     * @param $password
     * @param $request
     * @return array
     * @throws \Exception
     */
    public function curlCreateMember($accountUserName, $password, $request)
    {
        $params = [
            'agentCode' => $this->agentCode,
            'loginId' => $accountUserName,
        ];
        $output = $this->pbRequest('POST', self::API_PLAYER_CREATE, $params);

        if (array_key_exists('userCode', $output)) {
            return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password], 'message' => ''];
        } else {
            if (array_key_exists('code', $output)) {
                return ['success' => false, 'data' => [], 'message' => '创建用户失败，返回码：' . $output['message']];
            } else {
                return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
            }
        }

    }

    /**
     * gameapi
     * 2018/9/11 0:08
     * Administrator
     * curlGetBalance
     * 获取余额
     * @param $accountUserName
     * @return array|mixed
     */
    public function curlGetBalance($accountUserName)
    {
        $params = [
            'userCode' => $accountUserName,
        ];
        $output = $this->pbRequest('GET', self::API_PLAYER_BALANCE, $params);

        if (array_key_exists('userCode', $output)) {
            return ['success' => true, 'data' => ['balance' => $output['availableBalance'],], 'message' => ''];
        } else {
            if (array_key_exists('code', $output)) {
                return ['success' => false, 'data' => [], 'message' => '查询用户失败，返回码：' . $output['message']];
            } else {
                return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
            }
        }

    }

    /**
     * gameapi
     * 2018/12/3 16:58
     * Administrator
     * curlTransferIn
     * 账号转入
     * @param $accountUserName
     * @param $price
     * @param $transferid
     * @return array
     * @throws \Exception
     */
    public function curlTransferIn($accountUserName, $price, $transferid)
    {
        $params = [
            'userCode' => $accountUserName,
            'amount' => $price,
        ];
        $output = $this->pbRequest('POST', self::API_TRANSFER_IN, $params);

        if (array_key_exists('userCode', $output)) {
            return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
        } else {
            if (array_key_exists('code', $output)) {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转账失败，失败错：' . $output['message']];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
            }
        }

    }

    /**
     * gameapi
     * 2018/9/11 0:09
     * Administrator
     * curlTransferTo
     * 额度转出
     * @param $accountUserName
     * @param $price
     * @param $transferid
     * @return array|mixed
     */
    public function curlTransferTo($accountUserName, $price, $transferid)
    {
        $params = [
            'userCode' => $accountUserName,
            'amount' => $price,
        ];
        $output = $this->pbRequest('POST', self::API_TRANSFER_OUT, $params);

        if (array_key_exists('userCode', $output)) {
            return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
        } else {
            if (array_key_exists('code', $output)) {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转账失败，失败错：' . $output['message']];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
            }
        }
    }

    /**
     * gameapi
     * 2018/9/11 0:10
     * Administrator
     * curlCheckTransfer
     * 检查订单状态
     * @param $request
     * @return array|mixed
     */
    public function curlCheckTransfer($request)
    {
        return ['success' => false, 'data' => [], 'message' => '此平台没有检查订单此功能'];
    }

    public function curlKick($request)
    {
        $params = [
            'userCode' => $request->accountUserName,
        ];
        $output = $this->pbRequest('POST', self::API_PLAYER_LOGOUT, $params);

        if (array_key_exists('status', $output)) {
            return ['success' => true, 'data' => [], 'message' => ''];
        } else {
            if (array_key_exists('code', $output)) {
                return ['success' => false, 'data' => [], 'message' => '账户退出失败，失败错：' . $output['message']];
            } else {
                return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
            }
        }
    }

    /**
     * gameapi
     * 2018/11/29 14:26
     * Administrator
     * curlJoinGame
     * 游戏登录
     * @param $request
     * @return array
     * @throws \Exception
     */
    public function curlJoinGame($request)
    {
        $mainGamePlat = MainGamePlat::where('main_game_plat_code', $request->mainGamePlat)->first();
        if (isset($request->quota_handicap)) {
            $oddType = QuotaHandicap::where([
                    ['code', '=', $request->quota_handicap],
                    ['plat_id', '=', $mainGamePlat->id],
                ]
            )->first();
        }

        $params = [
            'userCode' => $request->accountUserName,
            'locale' => 'zh-cn',
            'oddsFormat' => isset($oddType) ? $oddType->plat_code : 'HK', //默认香港盘
        ];

        $output = $this->pbRequest('POST', self::API_PLAYER_LOGIN, $params);

        if (array_key_exists('userCode', $output)) {
            return ['success' => true, 'data' => ['url' => $output['loginUrl']], 'message' => ''];
        } else {
            if (array_key_exists('code', $output)) {
                return ['success' => false, 'data' => [], 'message' => '进入游戏失败，错误原因：' . $output['message']];
            } else {
                return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
            }
        }

    }

    /**
     * gameapi
     * 2018/9/11 0:10
     * Administrator
     * curlJoinMobileGame
     * 手机端游戏登录
     * @param $request
     * @return array
     */
    public function curlJoinMobileGame($request)
    {
        return $this->joinGame($request);
    }

    /**
     * gameapi
     * 2018/11/22 19:22
     * Administrator
     * curlGetBetRecord
     * 获取记录
     * @param Carbon $startTime
     * @param Carbon $endTime
     * @return array
     */
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
        $data = [];
        $start_time = $startTime->toDateTimeString();
        $end_time = $endTime->toDateTimeString();

        $params = [
            'dateFrom' => $start_time,
            'dateTo' => $end_time,
            'filterBy' => 'event_date',
            'locale' => 'zh-cn',
        ];

        $result = $this->pbRequest('GET', self::API_BET_RECORD, $params);

        if (is_array($result)) {
            if (count($result) > 0) {
                $data = array_merge($data, $result);
            }
        }

        return $data;
    }
}